home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
ada
/
c01lab4.zip
/
LRMRDR
/
LRM_CHAP.ZIP
/
CHAP12.DOC
< prev
next >
Wrap
Text File
|
1992-04-21
|
42KB
|
1,032 lines
> 12. Generic Units
A generic unit is a program unit that is either a generic subprogram or a
generic package. A generic unit is a template, which is parameterized or
not, and from which corresponding (nongeneric) subprograms or packages can
be obtained. The resulting program units are said to be instances of the
original generic unit.
A generic unit is declared by a generic declaration. This form of
declaration has a generic formal part declaring any generic formal
parameters. An instance of a generic unit is obtained as the result of a
generic instantiation with appropriate generic actual parameters for the
generic formal parameters. An instance of a generic subprogram is a
subprogram. An instance of a generic package is a package.
Generic units are templates. As templates they do not have the properties
that are specific to their nongeneric counterparts. For example, a generic
subprogram can be instantiated but it cannot be called. In contrast, the
instance of a generic subprogram is a nongeneric subprogram; hence, this
instance can be called but it cannot be used to produce further instances.
References: declaration 3.1, generic actual parameter 12.3, generic
declaration 12.1, generic formal parameter 12.1, generic formal part 12.1,
generic instantiation 12.3, generic package 12.1, generic subprogram 12.1,
instance 12.3, package 7, program unit 6, subprogram 6
> 12.1 Generic Declarations
A generic declaration declares a generic unit, which is either a generic
subprogram or a generic package. A generic declaration includes a generic
formal part declaring any generic formal parameters. A generic formal
parameter can be an object; alternatively (unlike a parameter of a
subprogram), it can be a type or a subprogram.
generic_declaration ::= generic_specification;
generic_specification ::=
generic_formal_part subprogram_specification
| generic_formal_part package_specification
generic_formal_part ::= generic {generic_parameter_declaration}
generic_parameter_declaration ::=
identifier_list : [in [out]] type_mark [:= expression];
| type identifier is generic_type_definition;
| private_type_declaration
| with subprogram_specification [is name];
| with subprogram_specification [is <>];
generic_type_definition ::=
(<>) | range <> | digits <> | delta <>
| array_type_definition | access_type_definition
The terms generic formal object (or simply, formal object), generic formal
type (or simply, formal type), and generic formal subprogram (or simply,
formal subprogram) are used to refer to corresponding generic formal
parameters.
The only form of subtype indication allowed within a generic formal part is
a type mark (that is, the subtype indication must not include an explicit
constraint). The designator of a generic subprogram must be an identifier.
Outside the specification and body of a generic unit, the name of this
program unit denotes the generic unit. In contrast, within the declarative
region associated with a generic subprogram, the name of this program unit
denotes the subprogram obtained by the current instantiation of the generic
unit. Similarly, within the declarative region associated with a generic
package, the name of this program unit denotes the package obtained by the
current instantiation.
The elaboration of a generic declaration has no other effect.
Examples of generic formal parts:
generic -- parameterless
generic
SIZE : NATURAL; -- formal object
generic
LENGTH : INTEGER := 200; -- formal object with a default expression
AREA : INTEGER := LENGTH*LENGTH; -- formal object with a default expression
generic
type ITEM is private; -- formal type
type INDEX is (<>); -- formal type
type ROW is array(INDEX range <>) of ITEM; -- formal type
with function "<"(X, Y : ITEM) return BOOLEAN; -- formal subprogram
Examples of generic declarations declaring generic subprograms:
generic
type ELEM is private;
procedure EXCHANGE(U, V : in out ELEM);
generic
type ITEM is private;
with function "*"(U, V : ITEM) return ITEM is <>;
function SQUARING(X : ITEM) return ITEM;
Example of a generic declaration declaring a generic package:
generic
type ITEM is private;
type VECTOR is array (POSITIVE range <>) of ITEM;
with function SUM(X, Y : ITEM) return ITEM;
package ON_VECTORS is
function SUM (A, B : VECTOR) return VECTOR;
function SIGMA(A : VECTOR) return ITEM;
LENGTH_ERROR : exception;
end;
Notes:
Within a generic subprogram, the name of this program unit acts as the name
of a subprogram. Hence this name can be overloaded, and it can appear in a
recursive call of the current instantiation. For the same reason, this
name cannot appear after the reserved word new in a (recursive) generic
instantiation.
An expression that occurs in a generic formal part is either the default
expression for a generic formal object of mode in, or a constituent of an
entry name given as default name for a formal subprogram, or the default
expression for a parameter of a formal subprogram. Default expressions for
generic formal objects and default names for formal subprograms are only
evaluated for generic instantiations that use such defaults. Default
expressions for parameters of formal subprograms are only evaluated for
calls of the formal subprograms that use such defaults. (The usual
visibility rules apply to any name used in a default expression: the
denoted entity must therefore be visible at the place of the expression.)
Neither generic formal parameters nor their attributes are allowed
constituents of static expressions (see 4.9).
References: access type definition 3.8, array type definition 3.6,
attribute 4.1.4, constraint 3.3, declaration 3.1, designator 6.1,
elaboration has no other effect 3.1, entity 3.1, expression 4.4, function
6.5, generic instantiation 12.3, identifier 2.3, identifier list 3.2,
instance 12.3, name 4.1, object 3.2, overloading 6.6 8.7, package
specification 7.1, parameter of a subprogram 6.2, private type definition
7.4, procedure 6.1, reserved word 2.9, static expression 4.9, subprogram 6,
subprogram specification 6.1, subtype indication 3.3.2, type 3.3, type mark
3.3.2
> 12.1.1 Generic Formal Objects
The first form of generic parameter declaration declares generic formal
objects. The type of a generic formal object is the base type of the type
denoted by the type mark given in the generic parameter declaration. A
generic parameter declaration with several identifiers is equivalent to a
sequence of single generic parameter declarations, as explained in section
3.2.
A generic formal object has a mode that is either in or in out. In the
absence of an explicit mode indication in a generic parameter declaration,
the mode in is assumed; otherwise the mode is the one indicated. If a
generic parameter declaration ends with an expression, the expression is
the default expression of the generic formal parameter. A default
expression is only allowed if the mode is in (whether this mode is
indicated explicitly or implicitly). The type of a default expression must
be that of the corresponding generic formal parameter.
A generic formal object of mode in is a constant whose value is a copy of
the value supplied as the matching generic actual parameter in a generic
instantiation, as described in section 12.3. The type of a generic formal
object of mode in must not be a limited type; the subtype of such a
gener